//
//  StartScreen.m
//  MultiViewDemo
//
//  Created by Ngoc on 4/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "StartScreenController.h"
#import "Util.h"
#import "CoordinatingController.h"

@implementation StartScreenController
@synthesize aboutController;
@synthesize editimageController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)dealloc
{
    [aboutController release];
    
    [super dealloc];
}
static StartScreenController * sharedStartScreen=nil;

+(StartScreenController*) sharedInstance
{
    if(nil == sharedStartScreen )
    {
        sharedStartScreen = [[StartScreenController alloc]init];
    }
    return sharedStartScreen;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    if (self.aboutController.view.superview == nil) self.aboutController = nil;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"ip-bg.png"]];
    [super viewDidLoad];
}

- (IBAction)showAboutView:(id)sender 
{    
   // if(self.aboutController.view.superview == nil)
    //{
   // if (self.aboutController == nil)
   // {
        AboutController *about	 = [[AboutController alloc] initWithNibName:@"About" bundle:nil]; 
        about.parentController = self;
        self.aboutController = about;
        [about release];
    //} 
   // }

    [[SwitchViewController sharedInstance] switchView:self toView:aboutController];

}
-(IBAction) showPaintView:(id)sender
{
    [[SwitchViewController sharedInstance]switchView:self toView:[CoordinatingController sharedInstance].canvasViewController];
}
// Get image from Camera 
-(IBAction) getImageFromCamera:(id)sender
{
//    if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
//    {
//        btnGetFromCamera.hidden = YES;
//    }
    @try
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        [picker setDelegate: self];
        //[picker setAllowsEditing:YES];
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
    @catch(id Exception)
    {
        [Util showSimpleMessageBox:@"Camera is now unavailable" withTitle:@"Error"];
    }
}
// Get image from library
-(IBAction) getImageFromLibrary:(id)sender
{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
    else
    {
        [Util showSimpleMessageBox:@"Library is now unavailable" withTitle:@"Error"] ;
    }
}
//Change view to EditImage view
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    @try {
        [picker dismissModalViewControllerAnimated:NO];
     //   if(self.editimageController  == nil)
       // {
            //EditImageController *edit = [[EditImageController alloc]initWithNibName:@"EditImage" bundle:nil];
            self.editimageController = [EditImageController sharedInstance];
            self.editimageController.parentController = self;
           // [edit release];
        //}
        [[SwitchViewController sharedInstance] switchView:self toView:self.editimageController];
       [self.editimageController setInputData:image];

       
    }
    @catch (NSException *exception) {
        [Util showSimpleMessageBox:@"Error from imagePicker" withTitle:@"Error"];
    }

    
}

- (void)viewDidUnload
{
    [self.editimageController release];
    [self.aboutController release];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
